define([
  'jquery',
  'underscore',
  'backbone',
  'models/user/UserModel',
  'text!templates/user/editTemplate.html'
], function($, _, Backbone,UserModel, editTemplate){
		
	  var EditView = Backbone.View.extend({
	   	el: '<div>',
	   	initialize: function(options){
	   		this.id = options.user_id;
	   	},
		render: function(){	 
			this.$el.empty();  
			aa=JSON.parse(localStorage.getItem("arrUser"));
			for (var i =0; i< aa.length;  i++){
				if(aa[i].id==this.id) { user_e=aa[i]; break;}

			}  	
			
			user_e2= new UserModel(); 
			user_e2.set({id: user_e.id, first_name: user_e.first_name, last_name: user_e.last_name, age: user_e.age});
			var template = _.template( editTemplate, {user: user_e2} ); //khai bao 1  template giao dien trang search
			this.$el.html(template);
			
		   	return this.$el;
		},
		events: {
			"click input[id='save_button']": "doSave"
		},
		doSave: function(e){
			e.preventDefault();
		   	aa=JSON.parse(localStorage.getItem("arrUser"));
			for (var i =0; i< aa.length;  i++) {
				if(aa[i].id===parseInt(this.id)) {		        			
					aa[i].first_name=$("[name='firstname']").val();
					aa[i].last_name=$("[name='lastname']").val();
					aa[i].age=$("[name='age']").val();
					localStorage.setItem("arrUser", JSON.stringify(aa)); //save lai
					//this.id="undefined";
					break;
				}	
			}  
			location.href="#/user"; 
		}
	});

  return EditView;
  
});
